package com.company;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class Main {

    private static final int RUNNER_COUNT = 10;
    public static void main(String[] args) throws InterruptedException {
        concurrenceTest();
        //runnerBegin();
        //display();
    }
    public static void runnerBegin() throws InterruptedException {

        // 开始的倒数锁
        final CountDownLatch begin = new CountDownLatch(1);

        // 结束的倒数锁
        final CountDownLatch end = new CountDownLatch(5);

        // 十个线程相当于十个选手
        final ExecutorService exec = Executors.newFixedThreadPool(5);

        for (int index = 0; index < 5; index++) {
            final int NO = index + 1;
            Runnable run = new Runnable() {
                public void run() {
                    try {
                        // 如果当前计数为零，则此方法立即返回。
                        begin.await();
                        Thread.sleep((long) (Math.random() * 10000));
                        System.out.println("No." + NO + " arrived"+LocalTime.now());
                    } catch (InterruptedException e) {
                    } finally {
                        // 每个选手到达终点时，end就减一
                        end.countDown();
                    }
                }
            };
            exec.submit(run);
        }
        System.out.println("Game Start……");
        // begin减一，开始游戏
        begin.countDown();
        // 等待end变为0，即所有选手到达终点
        end.await();
        System.out.println("Game Ove……");
        exec.shutdown();


    }

    public static void concurrenceTest() {
        /**
         * 模拟高并发情况代码
         */
        final AtomicInteger atomicInteger = new AtomicInteger(0);
        final CountDownLatch countDownLatch = new CountDownLatch(1000); // 相当于计数器，当所有都准备好了，再一起执行，模仿多并发，保证并发量
        final CountDownLatch countDownLatch2 = new CountDownLatch(20); // 保证所有线程执行完了再打印atomicInteger的值
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        List<Integer> list = new ArrayList<Integer>();
        try {
            for (int i = 0; i < 20; i++) {
                executorService.submit(new Callable() {


                    @Override
                    public Object call() throws Exception {
                        System.out.println("Task"+LocalTime.now());
                        countDownLatch2.countDown();
                        list.add(1);
                        return 1;
                    }

                });
               // countDownLatch.countDown();
            }

            countDownLatch2.await();// 保证所有线程执行完
          //  System.out.println(atomicInteger);
            System.out.println(list.size());
            executorService.shutdown();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public static  void display(){
        final CountDownLatch latch = new CountDownLatch(3);

        final ExecutorService exec = Executors.newFixedThreadPool(5);
        for(int i=0;i<3;i++){
            exec.submit(()->{
                try {
                    System.out.println("子线程" + Thread.currentThread().getName()
                            + "正在执行");

                    Thread.sleep(3000);

                    System.out.println("子线程" + Thread.currentThread().getName()
                            + "执行完毕");
                    latch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        exec.shutdown();




        try {
            System.out.println("等待3个子线程执行完毕...");
            latch.await();
            System.out.println("3个子线程已经执行完毕");
            System.out.println("继续执行主线程");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
